import { useDictStore } from "@/store";

const useDict = (dictCode) => {
  const $api = inject("$api");
  const store = useDictStore();
  const list = ref([]);
  const getList = async () => {
    if (!store.dictListMap[dictCode]) {
      const { data } = await $api.system.getDictList(dictCode);
      if (!data || !Array.isArray(data)) return;
      store.dictListMap[dictCode] = data.map((i) => ({
        label: i.dictLabel,
        value: i.dictValue,
      }));
    }
    list.value = store.dictListMap[dictCode];
  };
  getList();
  return {
    list,
  };
};

export default useDict;
